﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace DataTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
			
			String strdata = 
			@"<?xml version='1.0' encoding='utf-8'?>
				<models>
					<model id='1' faction='demons' type='troop'>
						<name>Imps</name>
						<vp>13</vp>
						<groundspeed>6</groundspeed>
						<wounds>1</wounds>
						<resistmagic>2</resistmagic>
						<morale>6</morale>
						<armour>
							<rating>3</rating>
							<missilebonus>1</missilebonus>
						</armour>
						<slots>
							<slot type='melee'>
								<numattack>1</numattack>
								<skill>3</skill>
								<strength>1</strength>
							</slot>
							<slot type='ranged'>
								<name>Longbow</name>
								<range>27</range>
								<numattack>1</numattack>
								<skill>3</skill>
								<strength>2</strength>
							</slot>
						</slots>
					</model>
					<model id='2' faction='demons' type='hero'>
						<name>Daemon Price</name>
						<vp>140</vp>
						<groundspeed>8</groundspeed>
						<wounds>4</wounds>
						<resistmagic>5</resistmagic>
						<morale>10</morale>
						<armour>
							<rating>3</rating>
							<missilebonus>1</missilebonus>
						</armour>
						<slots>
							<slot type='melee'>
								<numattack>1</numattack>
								<skill>3</skill>
								<strength>1</strength>
							</slot>
							<slot type='ranged'>
								<name>Longbow</name>
								<range>27</range>
								<numattack>1</numattack>
								<skill>3</skill>
								<strength>2</strength>
							</slot>
							<slot type='magic'>
								<range>10</range>
								<skill>2</skill>
								<strength>4</strength>
							</slot>
						</slots>
					</model>
				</models>
			";
			
			XDocument doc = XDocument.Parse(strdata);
			var Models = from Model in doc.Descendants("model")
				select new {
					ID = Model.Attribute("id").Value,
					Faction = Model.Attribute("faction").Value,
					Type = Model.Attribute("type").Value,
					Name = Model.Element("name").Value,
					VP = Model.Element("vp").Value,
					Wounds = Model.Element("wounds").Value,
					Morale = Model.Element("morale").Value,
					ResistMagic = Model.Element("resistmagic").Value,
					/*
					Armour = ( 	from ArmourStat in Model.Element("armour")
								select new
								{
									Rating = ArmourStat.Element("rating").Value(),
									MissileBonus = ArmourStat.Element("missilebonus").Value(),
								}),
					*/
					MagicSlots = (	from Slot in Model.Element("slots").Elements()
									where Slot.Attribute("type").Value == "magic"
									select new 
									{
										Name = Slot.Element("name").Value,
										Range = Slot.Element("Range").Value,
										NumAttack = Slot.Element("numattack").Value,
										Skill = Slot.Element("skill").Value,
										Strength = Slot.Element("strength").Value,
									}),
					
					
					RangeSlots = (	from Slot in Model.Element("slots").Elements()
									where Slot.Attribute("type").Value == "range"
									select new 
									{
										Name = Slot.Element("name").Value,
										Range = Slot.Element("Range").Value,
										NumAttack = Slot.Element("numattack").Value,
										Skill = Slot.Element("skill").Value,
										Strength = Slot.Element("strength").Value,
									}),
					
					MeleeSlots = (	from Slot in Model.Element("slots").Elements()
									where Slot.Attribute("type").Value == "melee"
									select new 
									{
										Name = Slot.Element("name").Value,
										Range = Slot.Element("Range").Value,
										NumAttack = Slot.Element("numattack").Value,
										Skill = Slot.Element("skill").Value,
										Strength = Slot.Element("strength").Value,
									}),
				};

			string testString = "";
			foreach (var Model in Models)
			{
				testString += Model.Name + " (" + Model.Faction + ")\n";
				testString += "Type = " + Model.Type + "\n";
				testString += "VP = " + Model.VP + "\n";
				// testString += "Armour = [" + Model.Armour.Rating + ", " + Model.Armour.MissileBonus + "]\n";
				testString += "--------\n";
				// testString = Model.MeleeSlots.ToString();
			}
			
			textdata.Text = testString;
        }
    }
}
